안녕하세요 아래 문의건 처럼 포트가 연결 안되는 부분은 해결한거 같습니다.
원인은 브레드보드에 초음파센서 바로 뒤쪽으로 꼽히는 케이블4개를 제거하니 포트가 인식 되었습니다.
사진상 케이블 배선이 오류가 있는건가요? 확인 부탁드립니다.
그리고 유투브 영상을 보고 아래 코드를 작성하였는데 제대로 동작을 하는거 같지 않습니다.
영상내 경기장에서 장애물 감지시 후진하고 방향을 틀어 다시 동작하는 코드를 받고 싶습니다.
j940328@naver.com 이리로 보내주시면 감사하겠습니다.
--------------------------------------------------------------------------------------------------------------------------------------------------------
작성코드
#include <SoftwareSerial.h>
#include <AFMotor.h>
AF_DCMotor motor_L(1);
AF_DCMotor motor_R(4);
int i;
//초음파센서 출력핀(trig)과 입력핀(echo), 변수, 함수 선언 //
int TrigPin = A0;
int EchoPin = A1;
long duration, distance;
void Obstacle_Check();
void Distance();
void Forward();
void Backward();
void Right();
void Left();
void Stop();
void setup() {
Serial.begin(9600); // PC와의 시리얼 통신속도
Serial.println("Eduino Smart Car Start!");
pinMode(EchoPin, INPUT); // EchoPin 입력
pinMode(TrigPin, OUTPUT); // TrigPin 출력
motor_L.setSpeed(160);
motor_L.run(RELEASE);
motor_R.setSpeed(180);
motor_R.run(RELEASE);
}
void loop() {
Forward();
delay(100);
Obstacle_Check();
}
//장애물 확인 및 회피 방향 결정//
void Obstacle_Check() {
int val = random(2);
Distance();
Serial.println(distance);
while (distance < 200) {
if (distance < 180) {
Backward();
delay(250);
Stop();
delay(50);
Distance();
}
else{
if (val ==0) {
Right();
delay(400);
}
else if (val ==1) {
Left();
delay(400);
}
Distance();
}
}
}
//거리 감지//
void Distance() {
digitalWrite(TrigPin, LOW);
delay(2);
digitalWrite(TrigPin, HIGH); // trigPin에서 초음파 발생(echoPin도 HIGH)
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
duration = pulseIn(EchoPin, HIGH); // echoPin이 HIGH를 유지한 시간을 저장 한다.
distance = ((float)(340 * duration)/1000) /2;
delay(5);
}
//방향 제어 함수//
void Forward() {
motor_L.run(FORWARD); motor_R.run(FORWARD);
for (i=0; i<200; i=i+20) {
motor_L.setSpeed(i); motor_R.setSpeed(i);
delay(2);
}
for (i=0;i<200;i=i-20){
motor_L.setSpeed(i); motor_R.setSpeed(i);
delay(2);
}
}
void Backward() {
motor_L.run(BACKWARD); motor_R.run(BACKWARD);
for (i=0; i<200; i=i+20) {
motor_L.setSpeed(i); motor_R.setSpeed(i);
delay(2);
}
for (i=0;i<200;i=i-20){
motor_L.setSpeed(i); motor_R.setSpeed(i);
delay(2);
}
}
void Right() {
motor_L.run(FORWARD); motor_R.run(BACKWARD);
for (i=0;i<180;i=i+20){
//j = i*1.3; if(j>=200) j = 200;
motor_L.setSpeed(i); motor_R.setSpeed(i);
delay(2);
}
for (i=180; i<0; i=i-20){
motor_L.setSpeed(i); motor_R.setSpeed(i);
delay(2);
}
}
void Left() {
motor_L.run(BACKWARD); motor_R.run(FORWARD);
for (i=0;i<180;i=i+20){
//j = i*1.3; if(j>=200) j = 200;
motor_L.setSpeed(i); motor_R.setSpeed(i);
delay(2);
}
for (i=180; i<0; i=i-20){
motor_L.setSpeed(i); motor_R.setSpeed(i);
delay(2);
}
}
void Stop() {
motor_L.run(RELEASE); motor_R.run(RELEASE);
for (i=200; i>=0; i=i-20) {
motor_L.setSpeed(i); motor_R.setSpeed(i);
delay(2);
}
}
보시면 보드의 Vcc부분이 모듈의 GND로 들어가있네요
조립하는 거 다시 확인하셔서 이런 부분 바로 잡으시면 해결 되겠습니당